package com.wsl.HealthChallenge.utils;

import java.util.List;

import javax.jdo.JDOHelper;
import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import com.wsl.HealthChallenge.models.ContestEntry;
import com.wsl.HealthChallenge.models.UserEntry;

/**
 * Provides some utility functions to access the datastore.
 * @author mark
 */
public class DataStorageUtils {
    public static PersistenceManager getPersistenceManager() {
        return PMF.get().getPersistenceManager();
    }

    /**
     * Requests the challenge with the given id from the datastore.
     * @param contestId
     * @return the challenge with the given id or null if it does not exist
     */
    public static ContestEntry getContest(String contestId) {
        PersistenceManager pm = PMF.get().getPersistenceManager();
        pm.getFetchPlan().setMaxFetchDepth(-1);
        Query query = pm.newQuery(ContestEntry.class);
        query.setFilter("id == contestIdParam");
        query.declareParameters("String contestIdParam");
        List<ContestEntry> results = (List<ContestEntry>) query.execute(contestId);
        if (results == null || results.isEmpty()){
            return null;
        }
        return results.get(0);
    }

    /**
     * Inserts or updates the given contest in the data store.
     * @param contest
     */
    public static boolean insertOrUpdateContest(ContestEntry contest) {
        PersistenceManager pm = JDOHelper.getPersistenceManager(contest);
        if (pm == null) {
            pm = PMF.get().getPersistenceManager();
        }
        try {
            pm.makePersistent(contest);
        } catch(Exception e) {
            return false;
        } finally {
            pm.close();
        }
        return true;
    }

    /**
     * Deletes the given contest in the data store.
     * @param contest
     */
    public static void deleteChallenge(ContestEntry contest) {
        PersistenceManager pm = JDOHelper.getPersistenceManager(contest);
        if (pm == null) {
            pm = PMF.get().getPersistenceManager();
        }
        try {
            pm.deletePersistent(contest);
        } finally {
            pm.close();
        }
    }
    
    /**
     * Requests the user entry with the given id from the datastore.
     * @param userId
     * @return the user entry with the given id or null if it does not exist
     */
    public static UserEntry getUserEntry(String userId) {
      PersistenceManager pm = PMF.get().getPersistenceManager();
      Query query = pm.newQuery(UserEntry.class);
      query.setFilter("userId == userIdParam");
      query.declareParameters("String userIdParam");
      List<UserEntry> results = (List<UserEntry>) query.execute(userId);
      if (results == null || results.isEmpty()){
          return null;
      }
      return results.get(0);
    }

    /**
     * Inserts or updates the given user in the data store.
     * @param userEntry
     */
    public static void insertOrUpdateUser(UserEntry userEntry) {
        PersistenceManager pm = JDOHelper.getPersistenceManager(userEntry);
        if (pm == null) {
            pm = PMF.get().getPersistenceManager();
        }
        try {
            pm.makePersistent(userEntry);
        } finally {
            pm.close();
        }
    }
}